//
//  ClientListController.m
//  StarOrangeStaff
//
//  Created by lshenrong on 2020/6/23.
//  Copyright © 2020 lshenrong. All rights reserved.¡
//

#import "ClientListController.h"
#import "ClientListCell.h"

@interface ClientListController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

@property (nonatomic,strong) UISearchBar *searchBar;
@property (nonatomic,strong) NSMutableArray *searchArray;
@property (nonatomic,strong) NSMutableArray *indexArray;

@property (nonatomic,copy) NSString *searchKey;
@property (nonatomic,assign) BOOL isSearchMode;//是否是搜索模式下
@property (nonatomic, assign) NSInteger searchInputCount; //用户输入次数记录,用来控制延迟搜索请求
@property (nonatomic,assign) NSInteger currentSearchPageNumber;//搜索页面编号，从1开始

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) CedarButton *comfirmBtn;

@end

@implementation ClientListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"客户列表";
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
    
    [UIConfigure configRefreshInTableView:self.tableView target:self action:@selector(resetRefreshData)];
//    [self beginRefresh];
}

- (void)beginRefresh{
    [self.tableView.mj_header beginRefreshing];
}

- (void)addRefreshFooter{
    if(!self.tableView.mj_footer){
        [UIConfigure  configLoadMoreInTableView:self.tableView target:self action:@selector(loadMoreData)];
    }
}

-  (void)removeRefreshFooter{
    if (self.tableView.mj_footer) {
    [self.tableView.mj_footer removeFromSuperview];
    }
}

- (void)resetRefreshData {
    if (self.isSearchMode) {
        [self refreshClientWithSearch];
    }else{
        [self refreshClientList];
    }
}



#pragma mark - Table view data source

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
//    if (self.isSearchMode == YES) {
//        return nil;
//    }else {
//        return _indexArray;
//
//    }
    return @[@"A",@"B",@"A"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;//section头部高度
}

//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    if (self.isSearchMode) {
//        return [self.searchArray count];
//    }else{
//        return [self.tableDataExtend.listData count];
//    }
    return 3;;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ClientListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClientListCellIdentifier"];
   
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 添加一个删除按钮
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
    }];
    // 添加一个编辑按钮
    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"编辑"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
  }];
    topRowAction.backgroundColor = UIColorHex(0x666666);
    // 将设置好的按钮放到数组中返回
    return @[deleteRowAction, topRowAction];
}


#pragma mark  ---- Request

- (void)refreshClientWithSearch {
    
}

- (void)refreshClientList {
    
}

#pragma mark   UISearchBarDelegate


- (void)requestSearchKeyWorld:(NSNumber *)count{
    if (self.searchInputCount == [count integerValue]) {
        //[self.searchAPI stop];
        if (self.searchArray) {
            [self.searchArray removeAllObjects];
        }
        self.currentSearchPageNumber = 1;
        [self.tableView.mj_footer resetNoMoreData];
        [self refreshClientWithSearch];
    }
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if([searchText length] == 0){
        if (self.searchArray) {
            [self.searchArray removeAllObjects];
        }
        [self.tableView reloadData];
        return;
    }
    self.searchKey = searchBar.text;
    self.searchInputCount ++;
    [self performSelector:@selector(requestSearchKeyWorld:) withObject:@(self.searchInputCount) afterDelay:0.5f];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    searchBar.showsCancelButton = NO;
    searchBar.text = nil;
    [searchBar resignFirstResponder];
    self.searchBar.text = nil;
    self.tableView.backgroundView = nil;
    if(self.isSearchMode){
        self.isSearchMode = NO;
        self.searchKey = 0;
        if (self.searchArray) {
            [self.searchArray removeAllObjects];
        }
        //[self addRefreshFooter];
        [self.tableView.mj_footer resetNoMoreData];
        [self.tableView reloadData];
    }
}



- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    self.isSearchMode = YES;
    searchBar.showsCancelButton = YES;       //显示“取消”按钮
    [self setSearchBarText:searchBar];
    if (IsEmptyStr(searchBar.text)) {
        if (self.searchArray) {
            [self.searchArray removeAllObjects];
        }
        [self.tableView reloadData];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.isSearchMode) {
        if(fabs( scrollView.contentOffset.y)>20){
            [self.searchBar resignFirstResponder];
        }
    }
}

- (void)setSearchBarText:(UISearchBar *)search{
    for (id obj in [self.searchBar subviews]) {
        if ([obj isKindOfClass:[UIView class]]) {
            for (id obj2 in [obj subviews]) {
                if ([obj2 isKindOfClass:[UIButton class]]) {
                    UIButton *btn = (UIButton *)obj2;
                    [btn setTitle:@"取消" forState:UIControlStateNormal];
                    [btn setTitleColor:kBlackColor forState:UIControlStateNormal];
                }
            }
        }
    }
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [UIConfigure tableViewFromFactory];
        _tableView.rowHeight = [ClientListCell heightForCell];//固定行高
        _tableView.sectionIndexColor = kBlackColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[ClientListCell class] forCellReuseIdentifier:@"ClientListCellIdentifier"];
        
        UIView * searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 41)];
        searchView.backgroundColor = [UIColor clearColor];
        searchView.clipsToBounds = YES;
        [searchView addSubview:self.searchBar];
        
        _tableView.tableHeaderView = searchView;
        
        CGFloat height = 20;
        if (IsPhoneX) {
            height = 60;
        }
        
        UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height)];
        bottomView.backgroundColor = kAppBackgoundColor;
        _tableView.tableFooterView = bottomView;
    }
    return _tableView;
}




- (NSMutableArray *)searchArray{
    if (!_searchArray) {
        _searchArray = [NSMutableArray array];
    }
    return _searchArray;
}

- (UISearchBar *)searchBar{
    if(!_searchBar){
        _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(5, 10,SCREEN_WIDTH-10, 31)];
        _searchBar.delegate = self;
        _searchBar.placeholder = @"请输入客户名称";
        [_searchBar setContentMode:UIViewContentModeCenter];
        UITextField  *seachTextFild = (UITextField  *) [PlatformTools FindSpecifiedSubViewFromClassName:@"UITextField" inView:_searchBar];
        if(seachTextFild){
            seachTextFild.backgroundColor = [UIColor colorWithRGB:0xe3e3e3];
            //修改字体大小
            seachTextFild.font = PingFangZhongChangGuiForFont(14);
            seachTextFild.layer.cornerRadius = 16.0f;
            seachTextFild.layer.borderColor = [UIColor colorWithRGB:0xe3e3e3].CGColor;
            seachTextFild.layer.borderWidth = 1;
            seachTextFild.layer.masksToBounds = YES;
        }
        _searchBar.backgroundImage = [UIImage imageWithColor:kAppBackgoundColor];
    }
    return _searchBar;
}

- (CedarButton *)comfirmBtn{
    if (!_comfirmBtn){
        _comfirmBtn =  [CedarButton buttonWithImage:[UIImage imageNamed:@""]];
        @weakify(self)
        _comfirmBtn.clickEvent = ^{
            @strongify(self)
        };
    }
    return _comfirmBtn;
}


@end

